##Replication Code for "Are Stealth Democrats Really Committed to Democracy? Process Preferences, Revisited"

##Load data
##Set your filepath
cppdata<-read.csv("BWCH_cppdata.csv")

##Create scales
cppdata$protectivesc<-(cppdata$protlead1+cppdata$protlead2+cppdata$protlead3+cppdata$protlead4+cppdata$protlead5)/15
cppdata$smprotectivesc<-(cppdata$protlead3+cppdata$protlead5)/6


##Generate Cronbach's alphas in main text
library(psych)
full.protective<-cppdata[,c("protlead1","protlead2","protlead3","protlead4","protlead5")]
alpha(full.protective)

item2.protective<-cppdata[,c("protlead3","protlead5")]
alpha(item2.protective)

##Basic Descriptives in Table 1
table(cppdata$stldem3)
prop.table(table(cppdata$stldem3))

##Regression Models in Table 2
model1<-lm(stldemscalr ~ disagr + pid7r + ideor + white +female +age1r + incr + educr, cppdata)
summary(model1)
model2<-lm(stldemscalr ~ disagr+autscale+ pid7r + ideor + white +female +age1r + incr + educr, cppdata)
summary(model2)
model3<-lm(stldemscalr ~ disagr+autscale+protectivesc + pid7r + ideor + white +female +age1r + incr + educr, cppdata)
summary(model3)
model4<-lm(stldemscalr ~ disagr*protectivesc+autscale+ pid7r + ideor + white +female +age1r + incr + educr, cppdata)
summary(model4)
model5<-lm(stldemscalr ~ disagr+autscale+smprotectivesc + pid7r + ideor + white +female +age1r + incr + educr, cppdata)
summary(model5)
model6<-lm(stldemscalr ~ disagr*smprotectivesc+autscale+ pid7r + ideor + white +female +age1r + incr + educr, cppdata)
summary(model6)

##Generate marginal effects plot in Figure 1

##packages required
library(gtable)
library(grid)
library(gridExtra)
library(interplot)
library(ggplot2)


figvals<-interplot(model4,"disagr","protectivesc",plot=F,hist=T,xmin=0,xmax=1)
kd<-density(cppdata$protectivesc,na.rm=T)
desn<-data.frame(kd$x,kd$y)

fig1plot<-ggplot(figvals,aes(protectivesc,coef))+geom_line(data=figvals)+geom_ribbon(data=figvals,aes(ymin=lb,ymax=ub),alpha=.3,color='black')+ylab("Marginal Effect of Discomfort with Disagreement")+geom_hline(yintercept=0,linetype="dashed")+xlab("Protective Leader Scale")+ggtitle("Figure 1. Expedient Governance Preference")+theme_bw()+theme(plot.title = element_text(hjust = 0.5))

fig1plot

